﻿using System.Collections.Generic;
using System.Linq;
using SIG.Model;
using SIG.Repository;
using SIG.Infrastructure.Logging;
using SIG.Infrastructure.Cache;
using SIG.Model.Enums;
using SIG.Infrastructure.Configs;
using SIG.Resources.Admin;

namespace SIG.Service
{
    public class CategoryService :  EntityService<Category>, ICategoryService
    {
       
        private readonly ICategoryRepository _categoryRepository;
        private readonly ILoggingService _loggingService;
        private readonly ICacheService _cacheService;

        public CategoryService(ICategoryRepository categoryRepository, ILoggingService loggingService, ICacheService cacheService)
            : base(categoryRepository, cacheService, loggingService)
        {           
            _categoryRepository = categoryRepository;
            _loggingService = loggingService;
            _cacheService = cacheService;
        }

        //public  Category GetByIdWithProducts(int id )
        //{
        //    return _categoryRepository.GetByIdWithProducts(id);
        //}



        public IList<Category> GetAllCategories(ModelType modelType)
        {
       
            var key = $"{EntityNames.Category}_All_{modelType}";

            if (!SettingsManager.Category.EnableCaching)
            {
                return _categoryRepository.GetFilteredElements((c => c.ModelType == modelType && c.ParentId == null),(c=>c.ChildCategories)).OrderByDescending(c => c.Importance).ToList();
            }
            

            if (_cacheService.IsSet(key))
            {
                return (List<Category>)_cacheService.Get(key);
            }
           
            List<Category> categorys = _categoryRepository.GetFilteredElements((c => c.ModelType == modelType && c.ParentId == null), (c => c.ChildCategories)).OrderByDescending(c => c.Importance).ToList();
            _cacheService.Set(key, categorys, SettingsManager.Site.CacheDuration);        

            return categorys;
        }

        // public IList<Category> GetActiveCategories(ModelType modelType)
        // {
        //     var key = "Category_Active_" + modelType.ToString();

        //     List<Category> categorys;

        //     if (_cacheService.IsSet(key))
        //     {
        //         categorys = (List<Category>)_cacheService.Get(key);
        //     }
        //     else
        //     {
        //         categorys = _categoryRepository.GetActivedCategoriesWithChild(modelType).ToList();
        //         _cacheService.Set(key, categorys, 30);
        //     }

        //     return categorys;
        // }

        public IList<Category> GetCategoriesWithParent(ModelType modelType)
        {
            var key = $"{EntityNames.Category}_All_WithParent_{modelType}";

            if (!SettingsManager.Category.EnableCaching)
            {
                return _categoryRepository.GetFilteredElements((c => c.ModelType == modelType), (c => c.ParentCategory)).OrderByDescending(c => c.Importance).ToList();
            }

           

            if (_cacheService.IsSet(key))
            {
               return (List<Category>)_cacheService.Get(key);
            }
           
            List<Category> categorys = _categoryRepository.GetFilteredElements((c => c.ModelType == modelType),(c=>c.ParentCategory)).OrderByDescending(c => c.Importance).ToList();
            _cacheService.Set(key, categorys, SettingsManager.Category.CacheDuration);
          

            return categorys;

            
        }


    }
}
